﻿(function ($) {
    var settings = {
        'width': 300,
        'height': 240,
        'title': 'Progress',
        'resizable': false,
        'waitText': 'Please wait...'
    };

    var methods = {
        init: function (options) {
            return this.each(function () {
                if (options) {
                    $.extend(settings, options);
                }
            });
        },
        destroy: function () {
            return this.each(function () {
                $(this).unbind('.progressSpinner');
            });
        },
        open: function (options) {
            if (options) {
                $.extend(settings, options);
            }

            return this.each(function () {
                var d = $(this).dialog({
                    autoOpen: true,
                    width: settings.width,
                    height: settings.height,
                    position: [($(window).size.width / 2) / (settings.width / 2), ($(window).size.height / 2) / (settings.height / 2)],
                    resizable: settings.resizable,
                    title: settings.title,
                    modal: true,
                    open: function (event, ui) {
                        $(this).html(' \
                            <div> \
                                <strong>' + settings.waitText + '</strong> \
                            </div> \
                            <div id="container" style="display: table; height: 100%; width:100%"> \
                                <div id="cell" style="display: table-cell; vertical-align: middle; width: 100%"> \
                                    <div id="spinner"></div> \
                                </div> \
                            </div> \
                            \
                            <script type="text/javascript"> \
                                var opts = { \
                                    lines: 14,length: 8,width: 5,radius: 12,color: "#53A514",speed: 1,trail: 44,shadow: false \
                                }; \
                                var target = document.getElementById("spinner"); \
                                var spinner = new Spinner(opts).spin(target); \
                            </script>'
                        );
                    },
                    buttons: {
                        "Close": function () {
                            $(this).unbind('.progressSpinner');
                            $(this).dialog("close");
                        }
                    }
                });
            });
        },
        closeWindow: function () {
            return this.each(function () {
                $(this).unbind('.progressSpinner');
                $(this).dialog('close');
            });
        }
    };

    $.fn.progressSpinner = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.progressSpinner');
        }
    };
})(jQuery);
